import { useEffect } from "react";

// const debounce = (func: () => void, delay: number = 3000) => {
//   let timeout:any
//   return () => {
//     if (timeout) {
//       clearTimeout(timeout)
//     }
//     timeout = setTimeout(function() {
//       func()
//     }, delay)
//   }
// }

// 防抖钩子
export const useDebounce = (
  fn: () => void,
  dep: any[],
  delay: number = 100
) => {
  useEffect(() => {
    let timer: any;
    timer = setTimeout(fn, delay);
    return () => clearTimeout(timer); // 这里用到useEffect清除的能力 类似于componentWillUnmount
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [...dep]);
};
